package ysq.xyg.host.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.api.aliapi.AliSmsApi;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.ProProperties;
import ysq.xyg.host.dao.HostShowingsDao;
import ysq.xyg.host.model.po.HostShowings;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostShowingsVo;
import ysq.xyg.host.service.HostShowingsService;
import ysq.xyg.web.model.po.WebConfig;

import java.util.Date;
import java.util.List;

/**
 * 房产-专车看房service实现
 *
 * @author congcong tao
 * @date 2019-12-11 14:49:36
 */
@Service("hostShowingsService")
public class HostShowingsServiceImpl extends BaseServiceImpl<HostShowings> implements HostShowingsService {

    @Autowired
    private EhCacheService ehCacheService;
    @Autowired
    private HostShowingsDao hostShowingsDao;

    @Autowired
    public void setBaseDao(HostShowingsDao hostShowingsDao) {
        this.baseDao = hostShowingsDao;
    }

    /**
     * 分页查询
     *
     * @param hostShowingsVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:49:36
     */
    @Override
    public ReturnEntity page(HostShowingsVo hostShowingsVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostShowings> list = acceptList(hostShowingsVo);
        PageInfo<HostShowings> page = new PageInfo<HostShowings>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostShowingsVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:49:36
     */
    @Override
    public ReturnEntity addOrUpdate(HostShowingsVo hostShowingsVo, SysUser currentUser) {
        boolean isUpate = true;
        HostShowings hostShowings;
        if (StrUtil.isEmpty(hostShowingsVo.getId())) {
            hostShowings = new HostShowings(currentUser);
            isUpate = false;
        } else {
            hostShowings = selectByPrimaryKey(hostShowingsVo.getId());
            if (ObjectUtil.isNull(hostShowings)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostShowings.setUpdateTime(new Date());
        }
        hostShowings.setMemberId(hostShowingsVo.getMemberId());
        hostShowings.setPhone(hostShowingsVo.getPhone());
        hostShowings.setName(hostShowingsVo.getName());
        hostShowings.setSex(hostShowingsVo.getSex());
        hostShowings.setItem(hostShowingsVo.getItem());
        hostShowings.setStatus(hostShowingsVo.getStatus());
        hostShowings.setUseTime(DateUtil.parseDateTime(hostShowingsVo.getUseTime()));
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostShowings);
        } else {
            num = insert(hostShowings);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:49:36
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostShowings hostShowings = selectByPrimaryKey(id);
        if (hostShowings != null) {
            hostShowings.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostShowings.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostShowings);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostShowingsVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 14:49:36
     */
    @Override
    public List<HostShowings> acceptList(HostShowingsVo hostShowingsVo){
        Example example = new Example(HostShowings.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostShowingsVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostShowingsVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostShowingsVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostShowingsVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostShowingsVo.getName())) {
            criteria.andLike("name", "%" + hostShowingsVo.getName() + "%");
        }
        if (StringUtil.isNotEmpty(hostShowingsVo.getPhone())) {
            criteria.andLike("phone", "%" + hostShowingsVo.getPhone() + "%");
        }
        example.setOrderByClause(" update_time desc");
        return hostShowingsDao.selectByExample(example);
    }

    @Override
    public ReturnEntity saveHostShowings(HostShowingsVo hostShowingsVo) {
        HostShowings hostShowings = new HostShowings();
        hostShowings.setId(BaseUtil.uId());
        hostShowings.setAccountId(hostShowingsVo.getAccountId());
        hostShowings.setMemberId(hostShowingsVo.getMemberId());
        hostShowings.setPhone(hostShowingsVo.getPhone());
        hostShowings.setName(hostShowingsVo.getName());
        hostShowings.setSex(hostShowingsVo.getSex());
        hostShowings.setItem(hostShowingsVo.getItem());
        hostShowings.setStatus(hostShowingsVo.getStatus());
        hostShowings.setUseTime(DateUtil.parseDate(hostShowingsVo.getUseTime()));
        hostShowings.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        hostShowings.setCreateTime(new Date());
        hostShowings.setUpdateTime(hostShowings.getCreateTime());
        int num = insert(hostShowings);
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        // 需要发送短信提醒
        String sendSmsFlag = ProProperties.getInstance().getSendSmsFlag();
        if (StringUtils.equals("true", sendSmsFlag)) {
            WebConfig webConfig = ehCacheService.selectWebConfigByAccountId(hostShowingsVo.getAccountId());
            if (webConfig != null && StringUtils.isNotEmpty(webConfig.getHostManagePhone())) {
                String param = "{\"code\":\"000001\"}";
                AliSmsApi.send(webConfig.getHostManagePhone(), AliSmsApi.TEMPLATE_CODE, param);
            }
        }
        return new ReturnEntity();
    }
}
